package trycat.cn.pagelistview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

/**
 * Author: trycat <br>
 * Description: Unicorn <br>
 * Since: 2017/3/14 10:48 <br>
 */

public class ManagerFragment extends Fragment {

    private View headerView;

    private static final String[] PRINCIPAL = new String[]{"活动详情", "修改活动", "取消活动", "报名审核", "成员管理", "活动评价", "活动报告", "我的记录", "我的报名时间", "活动群聊"};
    private static final String[] VOLUNTEER = new String[]{"活动详情", "活动评价", "活动报告", "我的记录", "我的报名时间", "活动群聊"};
    private static final String[] PUBLISH = new String[]{"活动详情", "修改活动", "取消活动", "报名审核", "成员管理", "活动评价", "活动报告"};
    private static Map<String, Integer> map = new HashMap<>();

    static {
        map.put("活动详情", R.mipmap.ic_launcher);
        map.put("修改活动", R.mipmap.ic_launcher);
        map.put("取消活动", R.mipmap.ic_launcher);
        map.put("报名审核", R.mipmap.ic_launcher);
        map.put("成员管理", R.mipmap.ic_launcher);
        map.put("活动评价", R.mipmap.ic_launcher);
        map.put("活动报告", R.mipmap.ic_launcher);
        map.put("我的记录", R.mipmap.ic_launcher);
        map.put("我的报名时间", R.mipmap.ic_launcher);
        map.put("活动群聊", R.mipmap.ic_launcher);
    }

    private GridView gridView;

    public static Fragment newInstance() {
        return new ManagerFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.manager_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        headerView = view.findViewById(R.id.headerView);
        gridView = (GridView) view.findViewById(R.id.gridView);
        gridView.setAdapter(new GridViewAdapter(PRINCIPAL));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getActivity(), adapterView.getItemAtPosition(i) + "", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void setHeaderViewHeight(int height) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        headerView.setLayoutParams(params);
    }

    class GridViewAdapter extends BaseAdapter {

        private String[] strings;

        public GridViewAdapter(String[] strings) {
            this.strings = strings;
        }

        @Override
        public int getCount() {
            return strings.length;
        }

        @Override
        public String getItem(int i) {
            return strings[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_manager_fragment, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(strings[position]);
            holder.iv.setImageResource(map.get(strings[position]));
            return convertView;
        }

        class ViewHolder {

            ImageView iv;
            TextView tv;

            public ViewHolder(View view) {
                iv = (ImageView) view.findViewById(R.id.icon);
                tv = (TextView) view.findViewById(R.id.title);
            }
        }
    }
}
